// pages/searchPage/searchPage.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarActiveIndex: 0,
    navbarTitle: [
      "最新消息",
      "热度排名"
    ],
    searchPageContent: '',
    index:0,
    first:false,
    judge:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      thumbUrl: app.globalData.imageUrl //七牛图片路径前缀
    })
  },

  onInputValue: function(e){
    var that = this;
    var searchPageContent = e.detail.value;
    that.setData({
      searchPageContent: searchPageContent
    })
  },

  //第二次开始，点击按钮事件 并收集formID
  onItemList: function(e){
    var that = this;
    var searchPageContent = that.data.searchPageContent;
    var user_id = wx.getStorageSync('user_id');
    var formId = e.detail.formId;
    app.saveFormId(user_id, formId);
    that.onSearchItemList(searchPageContent);
  },

  //根据字段获取商品列表信息
  onSearchItemList: function (searchPageContent){
    var that = this;
    if (searchPageContent == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入内容',
      })
    } else {
      wx.request({
        url: app.globalData.targetUrl + '/search/item/',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'get',
        data: {
          'str': searchPageContent.replace("'", "")
        },
         
        success: function (res) {
          var items = res.data.data;
          var first;
          var judge;
          if(items.length==0)
          {
            that.setData({
              judge:true
            });
          }
          else{
            that.setData({
              judge: false
            });
          }
          that.setData({
            items: items,
            first: true
          });
          var userIdList = [];
          for(var i = 0; i < items.length; i++){
            userIdList[i] = items[i].user_id;
          }
          that.LoadingUserMsg(userIdList);
        }
      })
    }
  },

  //加载商品用户列表信息
  LoadingUserMsg: function (id_list) {
    var that = this;
    wx.request({
      url: app.globalData.targetUrl + 'users/',
      method: 'GET',
      data: {
        'user_list': id_list
      },
      success: function (res) {
        var users = res.data.data;
        that.setData({
          users: users
        });
      }
    })
  },
  onItemTap: function (e) {
    var that = this;
    var itemId = e.currentTarget.dataset.item_id;
    wx.navigateTo({
      url: '/pages/detail/detail?itemId=' + itemId,
    })
  },
})